################################
    PROJECT(rmc-xmms2 CXX C)
################################

# TODO  lower this to the actually minimum-required version
CMAKE_MINIMUM_REQUIRED(VERSION 2.4.3)
set(CMAKE_VERBOSE_MAKEFILE OFF)

###################
#   Basic Setup   #

include(FindPkgConfig)

#cmake does something screwy with this, so we have to use the FORCE  -Luke
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "debug" CACHE STRING
        "Build type: Debug Release RelWithDebInfo MinSizeRel None"
        FORCE)
endif(NOT CMAKE_BUILD_TYPE)

include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})

###################################
#   Compilation Options/Targets   #

#   Types of User Interface
option(UI_CAIRO "Enable the Cairo-rendered User Interface" ON)
#option(UI_CLI "Enable a basic command line interface" OFF)
#option(UI_CURSES "Enable a basic text interface" OFF)

#   Target Windowing System(s)
#option(USE_... "Enable use of ..." OFF)
option(CAIRO_BACKEND_XLIB "" ON)
option(CAIRO_BACKEND_XCB "" OFF)
#option(CAIRO_BACKEND_WIN32 "Just use wine!" OFF)

#   Various
option(COVERART "Enable cover art images" ON)
option(OUTPUT_BUFFER "Enable output buffering, where possible" ON)
option(CUSTOM_LAYOUT "Customizable layout for the Cairo interface" ON)
#option(X_SYSTRAY "System tray icon in X" OFF)
#option(KDE4_PANEL "Enable an applet for the KDE4 panel" OFF)

######################################
#   Requirements of Chosen Options   #

set(FAYLYER false)
# TARG_LIB starts here

pkg_check_modules(XMMS2 xmms2-client)
if(XMMS2_FOUND)
    include_directories(${XMMS2_INCLUDE_DIRS})
    set(TARG_LIB ${XMMS2_LIBRARIES})
else(XMMS2_FOUND)
    set(FAYLYER true)
endif(XMMS2_FOUND)

if(UI_CAIRO)
    # Note: backends are treated as attempts rather than requirements.
    set(BACKEND_AVAILABLE FALSE)

    add_definitions(-DUI_CAIRO)
    pkg_check_modules(CAIRO cairo)
    include_directories(${CAIRO_INCLUDE_DIRS})
    set(TARG_LIB ${TARG_LIB} ${CAIRO_LIBRARIES})

    if(CAIRO_BACKEND_XLIB)
        add_definitions(-DCAIRO_XLIB)
        if(OUTPUT_BUFFER)
            add_definitions(-DPIXMAP_BUFFER)
        endif(OUTPUT_BUFFER)
        set(NEED_X11 true)
        pkg_check_modules(CAIRO_XLIB cairo-xlib)
        if(CAIRO_XLIB_FOUND)
            set(BACKEND_AVAILABLE TRUE)
            include_directories(${CAIRO_XLIB_INCLUDE_DIRS})
            set(TARG_LIB ${TARG_LIB} ${CAIRO_XLIB_LIBRARIES})
        endif(CAIRO_XLIB_FOUND)
    endif(CAIRO_BACKEND_XLIB)

    if(CAIRO_BACKEND_XLIB_XRENDER)
        set(NEED_X11 true)
        pkg_check_modules(CAIRO_XLIB_XRENDER cairo-xlib-xrender)
        if(CAIRO_XLIB_XRENDER_FOUND)
            set(BACKEND_AVAILABLE TRUE)
            add_definitions(-DCAIRO_XLIB_XRENDER)
            include_directories(${CAIRO_XLIB_XRENDER_INCLUDE_DIRS})
            set(TARG_LIB ${TARG_LIB} ${CAIRO_XLIB_XRENDER_LIBRARIES})
        endif(CAIRO_XLIB_XRENDER_FOUND)
    endif(CAIRO_BACKEND_XLIB_XRENDER)

    if(CAIRO_BACKEND_XCB)
        add_definitions(-DCAIRO_XCB)
        if(OUTPUT_BUFFER)
            add_definitions(-DPIXMAP_BUFFER)
        endif(OUTPUT_BUFFER)
        #set(NEED_X11 true)
        pkg_check_modules(XCB xcb-atom xcb-aux xcb-event xcb-icccm xcb-keysyms)
        include_directories(${XCB_INCLUDE_DIRS})
        set(TARG_LIB ${TARG_LIB} ${XCB_LIBRARIES})
        pkg_check_modules(CAIRO_XCB cairo-xcb)
        include_directories(${CAIRO_XCB_INCLUDE_DIRS})
        set(TARG_LIB ${TARG_LIB} ${CAIRO_XCB_LIBRARIES})
        if(XCB_FOUND AND CAIRO_XCB_FOUND)
            set(BACKEND_AVAILABLE TRUE)
        endif(XCB_FOUND AND CAIRO_XCB_FOUND)
    endif(CAIRO_BACKEND_XCB)

    if(NOT CAIRO_FOUND OR NOT BACKEND_AVAILABLE)
        set(FAYLYER true)
    endif(NOT CAIRO_FOUND OR NOT BACKEND_AVAILABLE)
endif(UI_CAIRO)

if(COVERART)
    find_package(JPEG)
    find_package(PNG)
    if(JPEG_FOUND OR PNG_FOUND)
        add_definitions(-DCOVERART)
        if(JPEG_FOUND)
            add_definitions(-DUSE_JPEG)
            include_directories(${JPEG_INCLUDE_DIR})
            set(TARG_LIB ${TARG_LIB} ${JPEG_LIBRARIES})
        endif(JPEG_FOUND)
        if(PNG_FOUND)
            add_definitions(-DUSE_PNG ${PNG_DEFINITIONS})
            include_directories(${PNG_INCLUDE_DIR})
            set(TARG_LIB ${TARG_LIB} ${PNG_LIBRARY})
        endif(PNG_FOUND)
    else(JPEG_FOUND OR PNG_FOUND)
        message(STATUS "Cover art disabled  (no image format)")
        set(COVERART FALSE)
    endif(JPEG_FOUND OR PNG_FOUND)
endif(COVERART)

if(NEED_X11)
    find_package(X11)
    if(X11_FOUND)
        include_directories(${X11_INCLUDE_DIR})
        set(TARG_LIB ${TARG_LIB} ${X11_LIBRARIES})
    else(X11_FOUND)
        set(FAYLYER true)
    endif(X11_FOUND)
endif(NEED_X11)

if(FAYLYER)
    message(SEND_ERROR, "Requirements of selected options not met.")
endif(FAYLYER)

####################
#   Source files   #

set(SOURCES
    mcp.cpp
    mcp.h
    client.cpp
    client.h
    utility.cpp
    utility.h
    preferences.cpp
    preferences.h
    configfile.cpp
    configfile.h
    ui.cpp
    ui.h
   )

if(UI_CAIRO)
    set(SOURCES ${SOURCES}
        ui_cairo.cpp
        ui_cairo.h
        uic_widget.cpp
        uic_widget.h
        uic_widgets.h
       )
    if(CAIRO_BACKEND_XLIB)
        set(SOURCES ${SOURCES} uic_xlib.cpp uic_xlib.h)
    endif(CAIRO_BACKEND_XLIB)
    if(CAIRO_BACKEND_XCB)
        set(SOURCES ${SOURCES} uic_xcb.cpp uic_xcb.h)
    endif(CAIRO_BACKEND_XCB)
    if(CUSTOM_LAYOUT)
        add_definitions(-DUIC_CUSTOM_LAYOUT)
        set(SOURCES ${SOURCES} uic_layout.cpp)
    endif(CUSTOM_LAYOUT)
    if(COVERART)
        set(SOURCES ${SOURCES} coverart.cpp coverart.h)
   endif(COVERART)
endif(UI_CAIRO)

#########################
#   Set the target(s)   #

if(NOT CMAKE_BUILD_TYPE STREQUAL "debug")
    # removes asserts
    add_definitions(-DNDEBUG)
else(NOT CMAKE_BUILD_TYPE STREQUAL "debug")
    add_definitions(-DDEBUG)
endif(NOT CMAKE_BUILD_TYPE STREQUAL "debug")

if(CMAKE_BUILD_TYPE STREQUAL "debug" OR
   CMAKE_BUILD_TYPE STREQUAL "relwithdebinfo")
    add_definitions(-Wall)
endif(CMAKE_BUILD_TYPE STREQUAL "debug" OR
      CMAKE_BUILD_TYPE STREQUAL "relwithdebinfo")

add_executable(rmc-xmms2 ${SOURCES})
target_link_libraries(rmc-xmms2 ${TARG_LIB})
install(TARGETS rmc-xmms2 DESTINATION bin)

####################################
#   Update the tags file (debug)   #
# FIXME  this should just silently run (or not)
if(CMAKE_BUILD_TYPE STREQUAL "debug")
    find_program(CTAGS ctags PATHS ENV PATH)
    if(CTAGS)
        mark_as_advanced(CTAGS)
        #add_custom_command(OUTPUT tags
        #                   COMMAND ${CTAGS} ${SOURCES}
        #                   DEPENDS ${SOURCES}
        #                   WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        #                  )
        add_custom_target(ctags DEPENDS tags
                          COMMAND ${CTAGS} ${SOURCES}
                          DEPENDS ${SOURCES}
                          WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
                         )
        add_dependencies(rmc-xmms2 DEPENDS ctags)
    endif(CTAGS)
endif(CMAKE_BUILD_TYPE STREQUAL "debug")

########################

# TODO  Ideally, limit includes to only the files that need them.
#       - Pretty sure this can be done with SET_SOURCE_FILES_PROPERTIES
